﻿using System;
using DevExpress.XtraEditors;
using System.Data;
using System.Data.SqlClient;
using DevExpress.XtraGrid.Views.Grid;
using System.Drawing;

namespace CekilisEkrani
{
    public partial class frmCekilisEkrani : XtraForm
    {
        private DataTable _cekilisler = new DataTable();
        private string cnnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
        SqlConnection _cnn;

        public frmCekilisEkrani()
        {
            InitializeComponent();
            _cekilisler.Columns.Add("No", typeof(string));
            _cekilisler.Columns.Add("Çekiliş Yapan Firma", typeof(string));
            _cekilisler.Columns.Add("Çekiliş Tarihi", typeof(DateTime));
            _cekilisler.Columns.Add("Hediye", typeof(string));
            _cekilisler.Columns.Add("Kalan Süre", typeof(string));
            _cekilisler.Columns.Add("Çekiliş Kazanan Firma", typeof(string));
            _cekilisler.Columns.Add("Çekiliş No", typeof(string));
            _cekilisler.Columns.Add("tamamlandi", typeof(bool));
        }

        private string KalanSure(DateTime tarih)
        {
            double fark = (tarih - DateTime.Now).TotalMinutes;
            int saat = Convert.ToInt32(fark) / 60;
            int dakika = Convert.ToInt32(fark - Convert.ToDouble(saat * 60));

            return string.Format("{0} Saat,{1} Dakika", saat, dakika);
        }

        private void RefreshRow()
        {
            using (SqlCommand cmd = new SqlCommand("SELECT cek.cekilis_no, CUSTOMERS.name, cek.cekilis_tarihi, cek.cekilis_aciklama, cek.cekilis_kazanan_firma,cek.cekilis_hareket_id,cek.cekilis_tamamlandi FROM CEKILISLER as cek WITH (NOLOCK) INNER JOIN CUSTOMERS WITH (NOLOCK) ON cek.tedarikci_kodu = CUSTOMERS.code", _cnn))
            {
                DateTime tarih;
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    tarih = rd.GetDateTime(2);
                    _cekilisler.Rows.Add(rd.GetString(0), rd.GetString(1), tarih, rd.GetString(3), KalanSure(tarih), (isNull(rd[4]) ?? ""), (isNull(rd[5]) ?? ""), (isNull(rd[6]) ?? false));
                }
                rd.Close();
            }
        }

        private object isNull(object deger)
        {
            if (deger == DBNull.Value)
            {
                return null;
            }
            return deger;
        }

        private void frmCekilisEkrani_Load(object sender, EventArgs e)
        {
            _cnn = new SqlConnection(cnnString);
            _cnn.Open();
            gridControl1.DataSource = _cekilisler;
            gridView1.Columns[7].Visible = false;
            gridView1.BestFitColumns();
        }

        private void yenileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshRow();
        }

        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.RowHandle >= 0)
            {
                string bitti = View.GetRowCellDisplayText(e.RowHandle, View.Columns["tamamlandi"]);
                if (bitti == "Unchecked")
                {
                    e.Appearance.BackColor = Color.LimeGreen;
                    e.Appearance.BackColor2 = Color.White;
                }
                else
                {
                    e.Appearance.BackColor = Color.DeepSkyBlue;
                    e.Appearance.BackColor2 = Color.White;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
            {
                backgroundWorker1.WorkerSupportsCancellation = true;
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            _cekilisler.Rows.Clear();
            RefreshRow();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            //gridView1.RefreshData();
            gridView1.BestFitColumns();
        }

        private void çekilişYapToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

    }
}
